package com.hyzh.latte.util;

import cn.hutool.core.collection.CollectionUtil;
import lombok.EqualsAndHashCode;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @author songkui
 * @since 2024/5/11 13:40
 */
public class GeoUtils {

    @EqualsAndHashCode(of ={"lat", "lng"})
    public static class Point {
        double lat;
        double lng;

        public Point(double lat, double lng) {
            this.lat = lat;
            this.lng = lng;
        }
    }

    private final static double EARTH_RADIUS = 6378.137;

    private static double rad(double d) {
        return d * Math.PI / 180.0;
    }

    /**
     * 根据两点间经纬度坐标（double值），计算两点间距离，单位为米
     */
    public static double computeDistance(Point p1, Point p2) {
        double radLat1 = rad(p1.lat);
        double radLat2 = rad(p2.lat);
        double a = radLat1 - radLat2;
        double b = rad(p1.lng) - rad(p2.lng);
        double s = 2 * Math.asin(Math.sqrt(
                Math.pow(Math.sin(a / 2), 2) + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
        s = s * EARTH_RADIUS;
        s = (s * 10000) / 10;
        return s;
    }


    public List<Point> computeGather(List<Point> sourceList, List<Point> modList, List<Point> gatherList, double rad, int number){

        if (CollectionUtil.isEmpty(modList) || modList.size() < rad){
            return gatherList;
        }

        Point point = modList.get(0);
        List<Point> temp = new ArrayList<>();
        for (Point p : sourceList){
            if ( !(computeDistance(point, p) < rad)){
                temp.add(p);
            }
        }

        if (temp.size() >= number){
            gatherList.add(point);
            modList.removeAll(temp);
        }

        return gatherList;

    }


    public static void main(String[] args) {
        System.out.println("距离差" + computeDistance( new Point(30.5352806, 114.3521917), new Point(30.535275, 114.352175) ) + "米");
    }




}
